import axios from "@/utils/axios";
import moment from "moment";
// 统一接口基础地址
const BASE_URL = "http://58.222.112.138:11000"; // 正式地址

/**
 * 登录
 */
// 导出一个名为login的函数，用于发送登录请求
// 登录和修改密码接口用原生 axios，不加 token
import axiosOrigin from "axios";
export function login(params) {
    const { username, password } = params;
    return axios({
        url: `${BASE_URL}/login/login/login`,
        method: "post",
        data: { username, password }
    });
}
// 导出一个函数，用于更新密码
export function updatePassword(params) {
    // 使用axios发送post请求，请求地址为BASE_URL/login/login/updatePassword，请求参数为params
    return axios({
        url: `${BASE_URL}/login/login/updatePassword`,
        method: "post",
        data: params
    });
}

// 创建 axios 实例
const instance = axiosOrigin.create({
  baseURL: BASE_URL,
  timeout: 5000
});

// 设置请求拦截器，自动加 token（登录和修改密码接口不加）
instance.interceptors.request.use(config => {
  // 只对非登录和非修改密码接口加 token
  const isLogin = config.url?.includes("/login/login/login");
  const isUpdatePwd = config.url?.includes("/login/login/updatePassword");
  if (!isLogin && !isUpdatePwd) {
    const token = localStorage.getItem("Token");
    console.log('[axios拦截器] 当前Token:', token); // 打印token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`; // 注意Bearer前缀
    }
    // 不手动加Cookie，浏览器自动带
    if (!config.headers["Content-Type"]) {
      config.headers["Content-Type"] = "application/json";
    }
    config.withCredentials = true; // 让浏览器自动带cookie
  }
  return config;
}, error => Promise.reject(error));

/**
 * 获取图形验证码
 */
export function getCode(params) {
	return instance({
		url: "/Index/getCaptchaCode",
		method: "post",
		data: params
	});
}

/**
 * 权限列表（侧边栏权限和按钮权限）
 * @param params
 */
export function getPermission(params) {
	return instance({
		url: "/Index/getPermission",
		method: "post",
		data: params
	});
}

/**
 * 账号列表
 * @param params
 */
export function getAdmintorList(params) {
	return instance({
		url: `/adminAuth/adminList`,
		method: "post",
		data: params
	});
}

/**
 * 员工档案
 * @param params
 */
export function getRoleList(params) {
	return instance({
		url: `${BASE_URL}/adminAuth/getRoleList`,
		method: "post",
		data: params
	});
}

// 上传图片
export function publicUploadFile(params) {
	return instance({
		url: "/public/uploadFile",
		method: "post",
		data: params
	});
}

// 用户查询API
export function getEmployeeList(params) {
  return instance({
    url: `${BASE_URL}/user/proofingSheet/queryUser`,
    method: "post",
    data: params
  });
}

// 员工分组下拉查询
export function getEmployeeGroupList(params) {
  return instance({
    url: `${BASE_URL}/user/proofingSheet/grouped`,
    method: "post",
    data: params
  });
}

// 新增员工档案API（弹窗用）
export function addEmployee(params) {
  return instance({
    url: `${BASE_URL}/user/proofingSheet/addUser`,
    method: "post",
    data: params
  });
}

// 员工档案查询API（新版）
export function employeeArchiveQuery(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/employeeQuery`,
    method: "post",
    data: params
  });
}



// 删除用户API
export function deleteUserApi(params) {
  return instance({
    url: `${BASE_URL}/user/proofingSheet/deleteUser`,
    method: "post",
    data: params
  });
}

/**
 * 创建员工档案
 * @param params
 */
export function createEmployee(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/create`,
    method: "post",
    data: params
  });
}

/**
 * 修改员工档案
 * @param params
 */
export function amendEmployee(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/amend`,
    method: "post",
    data: params
  });
}

/**
 * 获取用户角色列表
 * @param params
 */
// export function getUserRoles(params) {
//   return instance({
//     url: `/user/roles`, // 请根据实际后端接口调整
//     method: "post",
//     data: params
//   });
// }

/**
 * 获取部门列表
 * @param params
 */
export function getDepartmentListApi(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/departmentQueryInterface`,
    method: "post",
    data: params
  });
}
export { getDepartmentListApi as getDepartmentList };

/**
 * 获取民族列表
 * @param params { nationName?: string }
 */
export function getEthnicGroupList(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/queryEthnicGroup`,
    method: "post",
    data: params
  });
}

/**
 * 获取省份列表
 * @param params { provinceName?: string }
 */
export function getProvinceList(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/provinceName`,
    method: "post",
    data: params
  });
}

/**
 * 获取市区列表
 * @param params { cityName?: string, provinceCode?: string }
 */
export function getCityDistrictList(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/cityDistrict`,
    method: "post",
    data: params
  });
}

/**
 * 获取县区列表
 * @param params { countyName?: string, cityCode?: string }
 */
export function getCountyDistrictList(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/countyDistrict`,
    method: "post",
    data: params
  });
}

/**
 * 删除员工档案
 * @param params { FID: string }
 */
export function deleteEmployee(params) {
  return instance({
    url: `${BASE_URL}/employee/employee/batchDelete`,
    method: "post",
    data: params
  });
}